home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSTRAP.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  25KB  |  885 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/dostrap.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "scheme.h"
  36. #include "os.h"
  37. #include "msdos.h"
  38. #include "dostrap.h"
  39. #include "dosexcp.h"
  40.  
  41. extern void EXFUN (DOS_initialize_trap_recovery, (void));
  42. CONST char * EXFUN (find_trap_name, (int trapno));
  43. extern PTR initial_C_stack_pointer;
  44.  
  45. static enum trap_state trap_state;
  46. static enum trap_state user_trap_state;
  47.  
  48. static enum trap_state saved_trap_state;
  49. static int saved_trapno;
  50. static SIGINFO_T saved_info;
  51. static struct FULL_SIGCONTEXT * saved_scp;
  52.  
  53. static unsigned short
  54.   initial_C_ss = 0,
  55.   initial_C_ds = 0,
  56.   initial_C_cs = 0;
  57.  
  58. static void EXFUN (initialize_dos_trap_codes, (void));
  59. static void EXFUN
  60.   (continue_from_trap,
  61.    (int trapno, SIGINFO_T info, struct FULL_SIGCONTEXT * scp));
  62.  
  63. void
  64. DEFUN_VOID (DOS_initialize_trap_recovery)
  65. {
  66.   extern unsigned short getSS (void);
  67.  
  68.   initial_C_ss = (getSS ());
  69.   initial_C_ds = (getDS ());
  70.   initial_C_cs = (getCS ());
  71.   trap_state = trap_state_recover;
  72.   user_trap_state = trap_state_recover;
  73.   initialize_dos_trap_codes ();
  74. }
  75.  
  76. enum trap_state
  77. DEFUN (OS_set_trap_state, (state), enum trap_state state)
  78. {
  79.   enum trap_state old_trap_state = user_trap_state;
  80.   user_trap_state = state;
  81.   trap_state = state;
  82.   return (old_trap_state);
  83. }
  84.  
  85. static void
  86. DEFUN_VOID (trap_normal_termination)
  87. {
  88.   trap_state = trap_state_exitting_soft;
  89.   termination_trap ();
  90. }
  91.  
  92. static void
  93. DEFUN_VOID (trap_immediate_termination)
  94. {
  95.   trap_state = trap_state_exitting_hard;
  96.   OS_restore_external_state ();
  97.   exit (1);
  98. }
  99.  
  100. static void
  101. DEFUN_VOID (trap_recover)
  102. {
  103.   if (WITHIN_CRITICAL_SECTION_P ())
  104.     {
  105.       CLEAR_CRITICAL_SECTION_HOOK ();
  106.       EXIT_CRITICAL_SECTION ({});
  107.     }
  108.   reset_interruptable_extent ();
  109.   continue_from_trap (saved_trapno, saved_info, saved_scp);
  110. }
  111.  
  112. void
  113. DEFUN (trap_handler, (message, trapno, info, scp),
  114.        CONST char * message AND
  115.        int trapno AND
  116.        SIGINFO_T info AND
  117.        struct FULL_SIGCONTEXT * scp)
  118. {
  119.   int code = ((SIGINFO_VALID_P (info)) ? (SIGINFO_CODE (info)) : 0);
  120.   Boolean constant_space_broken = (!(CONSTANT_SPACE_SEALED ()));
  121.   enum trap_state old_trap_state = trap_state;
  122.  
  123.   if (old_trap_state == trap_state_exitting_hard)
  124.   {
  125.     _exit (1);
  126.   }
  127.   else if (old_trap_state == trap_state_exitting_soft)
  128.   {
  129.     trap_immediate_termination ();
  130.   }
  131.   trap_state = trap_state_trapped;
  132.   if (WITHIN_CRITICAL_SECTION_P ())
  133.   {
  134.     fprintf (stdout,
  135.          "\n>> A %s has occurred within critical section \"%s\".\n",
  136.          message, (CRITICAL_SECTION_NAME ()));
  137.     fprintf (stdout, ">> [exception %d (%s), code %d = 0x%x]\n",
  138.          trapno, (find_trap_name (trapno)), code, code);
  139.   }
  140.   else if (constant_space_broken || (old_trap_state != trap_state_recover))
  141.   {
  142.     fprintf (stdout, "\n>> A %s (%d) has occurred.\n", message, trapno);
  143.     fprintf (stdout, ">> [exception %d (%s), code %d = 0x%x]\n",
  144.          trapno, (find_trap_name (trapno)), code, code);
  145.   }
  146.   if (constant_space_broken)
  147.   {
  148.     fputs (">> Constant space has been overwritten.\n", stdout);
  149.     fputs (">> Probably a runaway recursion has overflowed the stack.\n",
  150.        stdout);
  151.   }
  152.   fflush (stdout);
  153.  
  154.   switch (old_trap_state)
  155.   {
  156.   case trap_state_trapped:
  157.     if ((saved_trap_state == trap_state_recover) ||
  158.     (saved_trap_state == trap_state_query))
  159.     {
  160.       fputs (">> The trap occurred while processing an earlier trap.\n",
  161.          stdout);
  162.       fprintf (stdout,
  163.            ">> [The earlier trap raised exception %d (%s), code %d.]\n",
  164.            saved_trapno,
  165.            (find_trap_name (saved_trapno)),
  166.            ((SIGINFO_VALID_P (saved_info))
  167.         ? (SIGINFO_CODE (saved_info))
  168.         : 0));
  169.       fputs (((WITHIN_CRITICAL_SECTION_P ())
  170.           ? ">> Successful recovery is extremely unlikely.\n"
  171.           : ">> Successful recovery is unlikely.\n"),
  172.          stdout);
  173.       break;
  174.     }
  175.     else
  176.       trap_immediate_termination ();
  177.   case trap_state_recover:
  178.     if ((WITHIN_CRITICAL_SECTION_P ()) || constant_space_broken)
  179.     {
  180.       fputs (">> Successful recovery is unlikely.\n", stdout);
  181.       break;
  182.     }
  183.     else
  184.     {
  185.       saved_trap_state = old_trap_state;
  186.       saved_trapno = trapno;
  187.       saved_info = info;
  188.       saved_scp = scp;
  189.       trap_recover ();
  190.     }
  191.   case trap_state_exit:
  192.     termination_trap ();
  193.   }
  194.  
  195.   fflush (stdout);
  196.   saved_trap_state = old_trap_state;
  197.   saved_trapno = trapno;
  198.   saved_info = info;
  199.   saved_scp = scp;
  200.     
  201.   while (1)
  202.   {
  203.     char option;
  204.     static CONST char * trap_query_choices[] =
  205.     {
  206.       "I = terminate immediately",
  207.       "N = terminate normally",
  208.       "R = attempt recovery",
  209.       "Q = terminate normally",
  210.       0
  211.       };
  212.     option = (userio_choose_option
  213.           ("Choose one of the following actions:",
  214.            "Action -> ",
  215.            trap_query_choices));
  216.     switch (option)
  217.     {
  218.       case 'I':
  219.         trap_immediate_termination ();
  220.       case '\0':
  221.         /* Error in IO. Assume everything scrod. */
  222.       case 'N':
  223.       case 'Q':
  224.         trap_normal_termination ();
  225.       case 'R':
  226.         trap_recover ();
  227.     }
  228.   }
  229. }
  230.  
  231. #define STATE_UNKNOWN        (LONG_TO_UNSIGNED_FIXNUM (0))
  232. #define STATE_PRIMITIVE        (LONG_TO_UNSIGNED_FIXNUM (1))
  233. #define STATE_COMPILED_CODE    (LONG_TO_UNSIGNED_FIXNUM (2))
  234. #define STATE_PROBABLY_COMPILED    (LONG_TO_UNSIGNED_FIXNUM (3))
  235.  
  236. struct trap_recovery_info
  237. {
  238.   SCHEME_OBJECT state;
  239.   SCHEME_OBJECT pc_info_1;
  240.   SCHEME_OBJECT pc_info_2;
  241.   SCHEME_OBJECT extra_trap_info;
  242. };
  243.  
  244. static struct trap_recovery_info dummy_recovery_info =
  245. {
  246.   STATE_UNKNOWN,
  247.   SHARP_F,
  248.   SHARP_F,
  249.   SHARP_F
  250. };
  251.  
  252. struct dos_trap_code_desc
  253. {
  254.   int trapno;
  255.   unsigned long code_mask;
  256.   unsigned long code_value;
  257.   char *name;
  258. };
  259.  
  260. static struct dos_trap_code_desc dos_trap_codes [64];
  261.  
  262. #define DECLARE_DOS_TRAP_CODE(s, m, v, n)                \
  263. {                                    \
  264.   ((dos_trap_codes [i]) . trapno) = (s);                \
  265.   ((dos_trap_codes [i]) . code_mask) = (m);                \
  266.   ((dos_trap_codes [i]) . code_value) = (v);                \
  267.   ((dos_trap_codes [i]) . name) = (n);                    \
  268.   i += 1;                                \
  269. }
  270.  
  271. static SCHEME_OBJECT
  272. DEFUN (find_trap_code_name, (trapno, info, scp),
  273.        int trapno AND
  274.        SIGINFO_T info AND
  275.        struct FULL_SIGCONTEXT * scp)
  276. {
  277.   unsigned long code = 0;
  278.   char * name = 0;
  279.   if (SIGINFO_VALID_P (info))
  280.     {
  281.       code = (SIGINFO_CODE (info));
  282.       {
  283.     struct dos_trap_code_desc * entry = (& (dos_trap_codes [0]));
  284.     while ((entry -> trapno) != DOS_INVALID_TRAP)
  285.       if (((entry -> trapno) == trapno)
  286.           && (((entry -> code_mask) & code) == (entry -> code_value)))
  287.       {
  288.         name = (entry -> name);
  289.         break;
  290.       }
  291.       else
  292.         entry += 1;
  293.       }
  294.     }
  295.   return (cons ((long_to_integer ((long) code)),
  296.         ((name == 0) ? SHARP_F
  297.          : (char_pointer_to_string ((unsigned char *) name)))));
  298. }
  299.  
  300. static void
  301. DEFUN_VOID (initialize_dos_trap_codes)
  302. {
  303.   unsigned int i = 0;
  304.  
  305.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Integer_divide_by_zero,
  306.              0, 0,
  307.              "Integer divide by zero");
  308.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Debug_exception,
  309.              0, 0,
  310.              "Debug exception");
  311.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Non_maskable_interrupt,
  312.              0, 0,
  313.              "Non-maskable interrupt");
  314.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Breakpoint,
  315.              0, 0,
  316.              "Breakpoint");
  317.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Integer_overflow,
  318.              0, 0,
  319.              "Integer overflow");
  320.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Bounds_check,
  321.              0, 0,
  322.              "Bounds check");
  323.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Invalid_opcode,
  324.              0, 0,
  325.              "Invalid opcode");
  326.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Numeric_co_processor_not_available,
  327.              0, 0,
  328.              "Numeric co-processor not available");
  329.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Double_fault,
  330.              0, 0,
  331.              "Double fault");
  332.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Numeric_co_processor_segment_overrun,
  333.              0, 0,
  334.              "Numeric co-processor segment overrun");
  335.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Invalid_TSS,
  336.              0, 0,
  337.              "Invalid TSS");
  338.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Segment_not_present,
  339.              0, 0,
  340.              "Segment not present");
  341.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Stack_exception,
  342.              0, 0,
  343.              "Stack exception");
  344.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_General_protection,
  345.              0, 0,
  346.              "General protection");
  347.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Page_Fault,
  348.              0, 0,
  349.              "Page Fault");
  350.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Floating_point_exception,
  351.              0, 0,
  352.              "Floating-point exception");
  353.   DECLARE_DOS_TRAP_CODE (DOS_EXCP_Alignment_check,
  354.              0, 0,
  355.              "Alignment check");
  356.   DECLARE_DOS_TRAP_CODE (DOS_INVALID_TRAP, 0, 0, ((char *) 0));
  357.   return;
  358. }
  359.  
  360. static CONST char *
  361. trap_names[NUM_DOS_EXCP] =
  362. {
  363.   "Integer divide by zero",
  364.   "Debugging trap",
  365.   "NMI interrupt",
  366.   "Breakpoint exception",
  367.   "INTO -- integer overflow",
  368.   "BOUND -- range exceeded",
  369.   "UD -- invalid opcode",
  370.   "NM -- 387 not available",
  371.   "DF -- double fault",
  372.   "387 segment overrun",
  373.   "TS -- invalid TSS",
  374.   "NP -- segment not present",
  375.   "SS -- stack fault",
  376.   "GP -- general protection",
  377.   "PF -- page fault",
  378.   ((CONST char *) NULL),
  379.   "MF -- floating-point error",
  380.   "AC -- alignment check"
  381. };
  382.  
  383. CONST char *
  384. DEFUN (find_trap_name, (trapno), int trapno)
  385. {
  386.   static char buffer [64], * name;
  387.   if ((trapno >= 0) &&
  388.       (trapno < ((sizeof (trap_names)) / (sizeof (char *)))))
  389.   {
  390.     name = trap_names[trapno];
  391.     if ((name != ((char *) NULL))
  392.         && (name[0] != '\0'))
  393.       return ((CONST char *) name);
  394.   }
  395.   sprintf (buffer, "unknown exception %d", trapno);
  396.   return ((CONST char *) buffer);
  397. }
  398.  
  399. static void
  400. DEFUN (setup_trap_frame, (trapno, info, scp, trinfo, new_stack_pointer),
  401.        int trapno AND
  402.        SIGINFO_T info AND
  403.        struct FULL_SIGCONTEXT * scp AND
  404.        struct trap_recovery_info * trinfo AND
  405.        SCHEME_OBJECT * new_stack_pointer)
  406. {
  407.   SCHEME_OBJECT handler;
  408.   SCHEME_OBJECT trap_name, trap_code;
  409.   int stack_recovered_p = (new_stack_pointer != 0);
  410.   long saved_mask = (FETCH_INTERRUPT_MASK ());
  411.   SET_INTERRUPT_MASK (0);    /* To prevent GC for now. */
  412.   if ((! (Valid_Fixed_Obj_Vector ())) ||
  413.       ((handler = (Get_Fixed_Obj_Slot (Trap_Handler))) == SHARP_F))
  414.     {
  415.       fprintf (stderr, "There is no trap handler for recovery!\n");
  416.       fflush (stderr);
  417.       termination_trap ();
  418.     }
  419.   if (Free > MemTop)
  420.     Request_GC (0);
  421.  
  422.   trap_name =
  423.     ((trapno <= 0)
  424.      ? SHARP_F
  425.      : (char_pointer_to_string
  426.     ((unsigned char *) (find_trap_name (trapno)))));
  427.   trap_code = (find_trap_code_name (trapno, info, scp));
  428.   if (!stack_recovered_p)
  429.     {
  430.       Initialize_Stack ();
  431.      Will_Push (CONTINUATION_SIZE);
  432.       Store_Return (RC_END_OF_COMPUTATION);
  433.       Store_Expression (SHARP_F);
  434.       Save_Cont ();
  435.      Pushed ();
  436.     }
  437.   else
  438.     Stack_Pointer = new_stack_pointer;
  439.  Will_Push (7 + CONTINUATION_SIZE);
  440.   STACK_PUSH (trinfo -> extra_trap_info);
  441.   STACK_PUSH (trinfo -> pc_info_2);
  442.   STACK_PUSH (trinfo -> pc_info_1);
  443.   STACK_PUSH (trinfo -> state);
  444.   STACK_PUSH (BOOLEAN_TO_OBJECT (stack_recovered_p));
  445.   STACK_PUSH (trap_code);
  446.   STACK_PUSH (trap_name);
  447.   Store_Return (RC_HARDWARE_TRAP);
  448.   Store_Expression (long_to_integer (trapno));
  449.   Save_Cont ();
  450.  Pushed ();
  451.   if (stack_recovered_p
  452.       /* This may want to do it in other cases, but this may be enough. */
  453.       && (trinfo->state == STATE_COMPILED_CODE))
  454.     Stop_History ();
  455.  
  456.   History = (Make_Dummy_History ());
  457.  Will_Push (STACK_ENV_EXTRA_SLOTS + 2);
  458.   STACK_PUSH (trap_name);
  459.   STACK_PUSH (handler);
  460.   STACK_PUSH (STACK_FRAME_HEADER + 1);
  461.  Pushed ();
  462.   SET_INTERRUPT_MASK (saved_mask);
  463.   abort_to_interpreter (PRIM_APPLY);
  464. }
  465.  
  466. /* DOS_INVALID_TRAP is an invalid trap, it means a user requested reset. */
  467.  
  468. void
  469. DEFUN (hard_reset, (scp), struct FULL_SIGCONTEXT * scp)
  470. {
  471.   continue_from_trap (DOS_INVALID_TRAP, 0, scp);
  472. }
  473.  
  474. /* Called synchronously. */
  475.  
  476. void
  477. DEFUN_VOID (soft_reset)
  478. {
  479.   struct trap_recovery_info trinfo;
  480.   SCHEME_OBJECT * new_stack_pointer =
  481.     (((Stack_Pointer <= Stack_Top) && (Stack_Pointer > Stack_Guard))
  482.      ? Stack_Pointer
  483.      : 0);
  484.   if ((Regs[REGBLOCK_PRIMITIVE]) != SHARP_F)
  485.     {
  486.       (trinfo . state) = STATE_PRIMITIVE;
  487.       (trinfo . pc_info_1) = (Regs[REGBLOCK_PRIMITIVE]);
  488.       (trinfo . pc_info_2) =
  489.     (LONG_TO_UNSIGNED_FIXNUM (Regs[REGBLOCK_LEXPR_ACTUALS]));
  490.       (trinfo . extra_trap_info) = SHARP_F;
  491.     }
  492.   else
  493.     {
  494.       (trinfo . state) = STATE_UNKNOWN;
  495.       (trinfo . pc_info_1) = SHARP_F;
  496.       (trinfo . pc_info_2) = SHARP_F;
  497.       (trinfo . extra_trap_info) = SHARP_F;
  498.     }
  499.   if ((Free >= Heap_Top) || (Free < Heap_Bottom))
  500.     /* Let's hope this works. */
  501.     Free = MemTop;
  502.   setup_trap_frame (DOS_INVALID_TRAP, 0, 0, (&trinfo), new_stack_pointer);
  503. }
  504.  
  505. #if !defined(HAVE_SIGCONTEXT) || !defined(HAS_COMPILER_SUPPORT) || defined(USE_STACKLETS)
  506.  
  507. static void
  508. DEFUN (continue_from_trap, (trapno, info, scp),
  509.        int trapno AND
  510.        SIGINFO_T info AND
  511.        struct FULL_SIGCONTEXT * scp)
  512. {
  513.   if (Free < MemTop)
  514.     Free = MemTop;
  515.   setup_trap_frame (trapno, info, scp, (&dummy_recovery_info), 0);
  516. }
  517.  
  518. #else /* HAVE_SIGCONTEXT and HAS_COMPILER_SUPPORT and not USE_STACKLETS */
  519.  
  520. /* Heuristic recovery from processor traps/exceptions.
  521.  
  522.    continue_from_trap attempts to:
  523.  
  524.    1) validate the trap information (pc and sp);
  525.    2) determine whether compiled code was executing, a primitive was
  526.       executing, or execution was in the interpreter;
  527.    3) guess what C global state is still valid; and
  528.    4) set up a recovery frame for the interpreter so that debuggers can
  529.       display more information. */
  530.  
  531. #include "gccode.h"
  532.  
  533. #define SCHEME_ALIGNMENT_MASK        ((sizeof (long)) - 1)
  534. #define STACK_ALIGNMENT_MASK        SCHEME_ALIGNMENT_MASK
  535. #define FREE_PARANOIA_MARGIN        0x100
  536.  
  537. /* PCs must be aligned according to this. */
  538.  
  539. #define PC_ALIGNMENT_MASK        ((1 << PC_ZERO_BITS) - 1)
  540.  
  541. /* But they may have bits that can be masked by this. */
  542.  
  543. #ifndef PC_VALUE_MASK
  544. #define PC_VALUE_MASK            (~0)
  545. #endif
  546.  
  547. #define C_STACK_SIZE            0x01000000
  548.  
  549. #ifdef HAS_COMPILER_SUPPORT
  550. #define ALLOW_ONLY_C 0
  551. #else
  552. #define ALLOW_ONLY_C 1
  553. #define PLAUSIBLE_CC_BLOCK_P(block)    0
  554. #endif
  555.  
  556. static SCHEME_OBJECT * EXFUN
  557.   (find_block_address, (char * pc_value, SCHEME_OBJECT * area_start));
  558.  
  559. #if 0
  560. #define get_etext() (&etext)
  561. #else
  562. /* For now */
  563. #define get_etext() (Heap_Bottom)
  564. #endif
  565.  
  566. static void
  567. DEFUN (continue_from_trap, (trapno, info, scp),
  568.        int trapno AND
  569.        SIGINFO_T info AND
  570.        struct FULL_SIGCONTEXT * scp)
  571. {
  572.   int pc_in_C;
  573.   int pc_in_heap;
  574.   int pc_in_constant_space;
  575.   int pc_in_scheme;
  576.   int pc_in_hyper_space;
  577.   int scheme_sp_valid;
  578.   long C_sp;
  579.   long scheme_sp;
  580.   long the_pc;
  581.   SCHEME_OBJECT * new_stack_pointer;
  582.   SCHEME_OBJECT * xtra_info;
  583.   struct trap_recovery_info trinfo;
  584.  
  585.   if (scp == ((struct FULL_SIGCONTEXT *) NULL))
  586.   {
  587.     if (Free < MemTop)
  588.       Free = MemTop;
  589.     setup_trap_frame (trapno, info, scp, (&dummy_recovery_info), 0);
  590.     /*NOTREACHED*/
  591.   }
  592.  
  593.   C_sp = (FULL_SIGCONTEXT_SP (scp));
  594.   scheme_sp = (FULL_SIGCONTEXT_SCHSP (scp));
  595.   the_pc = ((FULL_SIGCONTEXT_PC (scp)) & PC_VALUE_MASK);
  596.  
  597. #if FALSE
  598.   fprintf (stderr, "\ncontinue_from_trap:");
  599.   fprintf (stderr, "\tpc = 0x%08lx\n", the_pc);
  600.   fprintf (stderr, "\tCsp = 0x%08lx\n", C_sp);
  601.   fprintf (stderr, "\tssp = 0x%08lx\n", scheme_sp);
  602.   fprintf (stderr, "\tesp = 0x%08lx\n", Ext_Stack_Pointer);
  603. #endif
  604.  
  605.   if (((the_pc & PC_ALIGNMENT_MASK) != 0)
  606.       || (scp->sc_cs != initial_C_cs))
  607.   {
  608.     pc_in_C = 0;
  609.     pc_in_heap = 0;
  610.     pc_in_constant_space = 0;
  611.     pc_in_scheme = 0;
  612.     pc_in_hyper_space = 1;
  613.   }
  614.   else
  615.   {
  616.     pc_in_C = (the_pc <= ((long) (get_etext ())));
  617.     pc_in_heap =
  618.       ((the_pc < ((long) Heap_Top)) && (the_pc >= ((long) Heap_Bottom)));
  619.     pc_in_constant_space =
  620.       ((the_pc < ((long) Constant_Top)) &&
  621.        (the_pc >= ((long) Constant_Space)));
  622.     pc_in_scheme = (pc_in_heap || pc_in_constant_space);
  623.     pc_in_hyper_space = ((!pc_in_C) && (!pc_in_scheme));
  624.   }
  625.  
  626.   scheme_sp_valid =
  627.     (pc_in_scheme
  628.      && ((scp->sc_ss & 0xffff) == (scp->sc_ds & 0xffff))
  629.      && ((scp->sc_ds & 0xffff) == (initial_C_ds & 0xffff))
  630.      && ((scheme_sp < ((long) Stack_Top)) &&
  631.      (scheme_sp >= ((long) Absolute_Stack_Base)) &&
  632.      ((scheme_sp & STACK_ALIGNMENT_MASK) == 0)));
  633.  
  634.   new_stack_pointer =
  635.     (scheme_sp_valid
  636.      ? ((SCHEME_OBJECT *) scheme_sp)
  637.      : ((pc_in_C
  638.     && ((scp->sc_ss & 0xffff) == (initial_C_ss & 0xffff))
  639.     && (Stack_Pointer < Stack_Top)
  640.     && (Stack_Pointer > Absolute_Stack_Base))
  641.         ? Stack_Pointer
  642.         : ((SCHEME_OBJECT *) 0)));
  643.  
  644.   if (pc_in_hyper_space || (pc_in_scheme && ALLOW_ONLY_C))
  645.   {
  646.     /* In hyper space. */
  647.     (trinfo . state) = STATE_UNKNOWN;
  648.     (trinfo . pc_info_1) = SHARP_F;
  649.     (trinfo . pc_info_2) = SHARP_F;
  650.     new_stack_pointer = 0;
  651.     if ((Free < MemTop) ||
  652.     (Free >= Heap_Top) ||
  653.     ((((unsigned long) Free) & SCHEME_ALIGNMENT_MASK) != 0))
  654.       Free = MemTop;
  655.   }
  656.   else if (pc_in_scheme)
  657.   {
  658.     /* In compiled code. */
  659.     SCHEME_OBJECT * block_addr;
  660.     SCHEME_OBJECT * maybe_free;
  661.     block_addr =
  662.       (find_block_address (((PTR) the_pc),
  663.                (pc_in_heap ? Heap_Bottom : Constant_Space)));
  664.     if (block_addr == 0)
  665.     {
  666.       (trinfo . state) = STATE_PROBABLY_COMPILED;
  667.       (trinfo . pc_info_1) = (LONG_TO_UNSIGNED_FIXNUM (the_pc));
  668.       (trinfo . pc_info_2) = SHARP_F;
  669.       if ((Free < MemTop) ||
  670.       (Free >= Heap_Top) ||
  671.       ((((unsigned long) Free) & SCHEME_ALIGNMENT_MASK) != 0))
  672.     Free = MemTop;
  673.     }
  674.     else
  675.     {
  676.       (trinfo . state) = STATE_COMPILED_CODE;
  677.       (trinfo . pc_info_1) =
  678.     (MAKE_POINTER_OBJECT (TC_COMPILED_CODE_BLOCK, block_addr));
  679.       (trinfo . pc_info_2) =
  680.     (LONG_TO_UNSIGNED_FIXNUM (the_pc - ((long) block_addr)));
  681. #ifdef HAVE_FULL_SIGCONTEXT
  682.       maybe_free = ((SCHEME_OBJECT *) (FULL_SIGCONTEXT_RFREE (scp)));
  683.       if (((((unsigned long) maybe_free) & SCHEME_ALIGNMENT_MASK) == 0)
  684.       && (maybe_free >= Heap_Bottom) && (maybe_free < Heap_Top))
  685.     Free = (maybe_free + FREE_PARANOIA_MARGIN);
  686.       else
  687. #endif
  688.       {
  689.     if ((Free < MemTop) || (Free >= Heap_Top)
  690.         || ((((unsigned long) Free) & SCHEME_ALIGNMENT_MASK) != 0))
  691.       Free = MemTop;
  692.       }
  693.     }
  694.   }
  695.   else
  696.   {
  697.     /* In the interpreter, a primitive, or a compiled code utility. */
  698.  
  699.     SCHEME_OBJECT primitive = (Regs[REGBLOCK_PRIMITIVE]);
  700.  
  701.     if ((OBJECT_TYPE (primitive)) != TC_PRIMITIVE)
  702.     {
  703.       (trinfo . state) = STATE_UNKNOWN;
  704.       (trinfo . pc_info_1) = SHARP_F;
  705.       (trinfo . pc_info_2) = SHARP_F;
  706.       new_stack_pointer = 0;
  707.     }
  708.     else
  709.     {
  710.       long primitive_address =
  711.     ((long) (Primitive_Procedure_Table[OBJECT_DATUM (primitive)]));
  712.       (trinfo . state) = STATE_PRIMITIVE;
  713.       (trinfo . pc_info_1) = primitive;
  714.       (trinfo . pc_info_2) =
  715.     (LONG_TO_UNSIGNED_FIXNUM (Regs[REGBLOCK_LEXPR_ACTUALS]));
  716.     }
  717.     if ((new_stack_pointer == 0)
  718.     || ((((unsigned long) Free) & SCHEME_ALIGNMENT_MASK) != 0)
  719.     || ((Free < Heap_Bottom) || (Free >= Heap_Top))
  720.     || ((Free < MemTop) && ((Free + FREE_PARANOIA_MARGIN) >= MemTop)))
  721.       Free = MemTop;
  722.     else if ((Free + FREE_PARANOIA_MARGIN) < MemTop)
  723.       Free +=  FREE_PARANOIA_MARGIN;
  724.   }
  725.   xtra_info = Free;
  726.   Free += (1 + 2 + PROCESSOR_NREGS);
  727.   (trinfo . extra_trap_info) =
  728.     (MAKE_POINTER_OBJECT (TC_NON_MARKED_VECTOR, xtra_info));
  729.   (*xtra_info++) =
  730.     (MAKE_OBJECT (TC_MANIFEST_NM_VECTOR, (2 + PROCESSOR_NREGS)));
  731.   (*xtra_info++) = ((SCHEME_OBJECT) the_pc);
  732.   (*xtra_info++) = ((SCHEME_OBJECT) C_sp);
  733.   {
  734.     int counter = FULL_SIGCONTEXT_NREGS;
  735.     int * regs = (FULL_SIGCONTEXT_FIRST_REG (scp));
  736.     while ((counter--) > 0)
  737.       (*xtra_info++) = ((SCHEME_OBJECT) (*regs++));
  738.   }
  739.   /* We assume that regs,sp,pc is the order in the processor.
  740.      Scheme can always fix this. */
  741.   if ((PROCESSOR_NREGS - FULL_SIGCONTEXT_NREGS) > 0)
  742.     (*xtra_info++) = ((SCHEME_OBJECT) C_sp);
  743.   if ((PROCESSOR_NREGS - FULL_SIGCONTEXT_NREGS) > 1)
  744.     (*xtra_info++) = ((SCHEME_OBJECT) the_pc);
  745.   setup_trap_frame (trapno, info, scp, (&trinfo), new_stack_pointer);
  746. }
  747.  
  748. /* Find the compiled code block in area which contains `pc_value'.
  749.    This attempts to be more efficient than `find_block_address_in_area'.
  750.    If the pointer is in the heap, it can actually do twice as
  751.    much work, but it is expected to pay off on the average. */
  752.  
  753. static SCHEME_OBJECT * EXFUN
  754.   (find_block_address_in_area, (char * pc_value, SCHEME_OBJECT * area_start));
  755.  
  756. #define MINIMUM_SCAN_RANGE        2048
  757.  
  758. static SCHEME_OBJECT *
  759. DEFUN (find_block_address, (pc_value, area_start),
  760.        char * pc_value AND
  761.        SCHEME_OBJECT * area_start)
  762. {
  763.   if (area_start == Constant_Space)
  764.     {
  765.       extern SCHEME_OBJECT * EXFUN
  766.     (find_constant_space_block, (SCHEME_OBJECT *));
  767.       SCHEME_OBJECT * constant_block =
  768.     (find_constant_space_block
  769.      ((SCHEME_OBJECT *)
  770.       (((unsigned long) pc_value) &~ SCHEME_ALIGNMENT_MASK)));
  771.       return
  772.     ((constant_block == 0)
  773.      ? 0
  774.      : (find_block_address_in_area (pc_value, constant_block)));
  775.     }
  776.   {
  777.     SCHEME_OBJECT * nearest_word =
  778.       ((SCHEME_OBJECT *)
  779.        (((unsigned long) pc_value) &~ SCHEME_ALIGNMENT_MASK));
  780.     long maximum_distance = (nearest_word - area_start);
  781.     long distance = maximum_distance;
  782.     while ((distance / 2) > MINIMUM_SCAN_RANGE)
  783.       distance = (distance / 2);
  784.     while ((distance * 2) < maximum_distance)
  785.       {
  786.     SCHEME_OBJECT * block =
  787.       (find_block_address_in_area (pc_value, (nearest_word - distance)));
  788.     if (block != 0)
  789.       return (block);
  790.     distance *= 2;
  791.       }
  792.   }
  793.   return (find_block_address_in_area (pc_value, area_start));
  794. }
  795.  
  796. /*
  797.   Find the compiled code block in area which contains `pc_value',
  798.   by scanning sequentially the complete area.
  799.   For the time being, skip over manifest closures and linkage sections. */
  800.  
  801. static SCHEME_OBJECT *
  802. DEFUN (find_block_address_in_area, (pc_value, area_start),
  803.        char * pc_value AND
  804.        SCHEME_OBJECT * area_start)
  805. {
  806.   SCHEME_OBJECT * first_valid = area_start;
  807.   SCHEME_OBJECT * area = area_start;
  808.   while (((char *) area) < pc_value)
  809.     {
  810.       SCHEME_OBJECT object = (*area);
  811.       switch (OBJECT_TYPE (object))
  812.     {
  813.     case TC_LINKAGE_SECTION:
  814.       {
  815.         switch (READ_LINKAGE_KIND (object))
  816.         {
  817.           case OPERATOR_LINKAGE_KIND:
  818.           case GLOBAL_OPERATOR_LINKAGE_KIND:
  819.           {
  820.         long count = (READ_OPERATOR_LINKAGE_COUNT (object));
  821.         area = ((END_OPERATOR_LINKAGE_AREA (area, count)) + 1);
  822.         break;
  823.           }
  824.  
  825.           default:
  826. #if FALSE
  827.           {
  828.         gc_death (TERM_EXIT,
  829.               "find_block_address: Unknown compiler linkage kind.",
  830.               area, NULL);
  831.         /*NOTREACHED*/
  832.           }
  833. #else
  834.           /* Fall through, no reason to crash here. */
  835. #endif
  836.           case REFERENCE_LINKAGE_KIND:
  837.           case ASSIGNMENT_LINKAGE_KIND:
  838.             area += ((READ_CACHE_LINKAGE_COUNT (object)) + 1);
  839.         break;
  840.  
  841.         }
  842.         break;
  843.       }
  844.     case TC_MANIFEST_CLOSURE:
  845.       {
  846.         area += 1;
  847.         {
  848.           long count = (MANIFEST_CLOSURE_COUNT (area));
  849.           area = ((MANIFEST_CLOSURE_END (area, count)) + 1);
  850.         }
  851.         break;
  852.       }
  853.     case TC_MANIFEST_NM_VECTOR:
  854.       {
  855.         long count = (OBJECT_DATUM (object));
  856.         if (((char *) (area + (count + 1))) < pc_value)
  857.           {
  858.         area += (count + 1);
  859.         first_valid = area;
  860.         break;
  861.           }
  862.         {
  863.           SCHEME_OBJECT * block = (area - 1);
  864.           return
  865.         (((area == first_valid) ||
  866.           ((OBJECT_TYPE (*block)) != TC_MANIFEST_VECTOR) ||
  867.           ((OBJECT_DATUM (*block)) < (count + 1)) ||
  868.           (! (PLAUSIBLE_CC_BLOCK_P (block))))
  869.          ? 0
  870.          : block);
  871.         }
  872.       }
  873.     default:
  874.       {
  875.         area += 1;
  876.         break;
  877.       }
  878.     }
  879.     }
  880.   return (0);
  881. }
  882.  
  883. #endif /* HAVE_SIGCONTEXT and HAS_COMPILER_SUPPORT and not USE_STACKLETS */
  884.  
  885.